﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Lamex.Library;
using DevExpress.Utils;
using System.IO;
using Lamex.Interface;
using Lamex.Library.CommonClass;

namespace Lamex.mSJ.UI
{
    public partial class frmSJ_Accessory_Search : frmBaseChild
    {
        public frmSJ_Accessory_Search()
        {
            InitializeComponent();
        }

        private void frmSJ_Accessory_Search_Load(object sender, EventArgs e)
        {
            try { base.InitButtons(); } catch { }//初始化标准功能，因要应用于RibbonForm，所以加入异常处理

            panelControlSingleBox.Dock = DockStyle.Fill;
            panelControlSingleBox.Visible = true;
            panelControlMultiBox.Width = 0;
            panelControlMultiBox.Visible = false;
            panelControlNineBox.Width = 0;
            panelControlNineBox.Visible = false;
        }

        private void sbtnGetPath_Click(object sender, EventArgs e)
        {
            XtraFolderBrowserDialog folder = new XtraFolderBrowserDialog();
            FolderBrowserDialog fb = new FolderBrowserDialog();
            //设置默认根目录是桌面
            folder.RootFolder = Environment.SpecialFolder.Desktop;
            folder.SelectedPath = @"L:\MES\SysSource\Image\Accessory\";//用绝对路径时不能正确指向
            //folder.SelectedPath = @"D:\Temp\";
            //设置对话框说明
            folder.Description = "请选择文件搜索路径:";
            if (folder.ShowDialog() == DialogResult.OK)
            {
                txtSearchPath.Text = folder.SelectedPath;
            }
        }

        private void sbtnView_Click(object sender, EventArgs e)
        {
            //MdiTools.OpenChildForm(this.MdiParent as IMdiForm, typeof(frmSJ_Test), sender as ToolStripMenuItem);
            
            //左侧gridControl控件加载图像
            gridControlImageDataAdd();

            //重置（清空）所有图片控件显示
            //To Do .......
        }

        //gridControl单击事件
        private void gridControl1_Click(object sender, EventArgs e)
        {
            if (rbtnSingleBox.Checked == true)
            {
                addImage_SingleBox_Demo();
                //addImage_SingleBox_Mode();
            }
            else if (rbtnMultiBox.Checked == true)
            {
                addImage_MultiBox_Demo();
                //addImage_MultiBox_Mode();
            }
            else if (rbtnNineBox.Checked == true)
            {
                addImage_NineBox_Demo();
                //addImage_NineBox_Mode();
            }
        }

        #region 图像控件文件加载，开发演示

        private void addImage_SingleBox_Demo()
        {
            //从流中读取图片文件，避免文件占用导致所有者无法修改
            string imageFile = txtSearchPath.Text + "\\" + tboxFileName.Text + ".jpg";
            FileStream fileStream = new FileStream(imageFile, FileMode.Open, FileAccess.Read);
            pboxSingle.Image = Image.FromStream(fileStream);
            fileStream.Close();
            fileStream.Dispose();

            Bitmap bmp = new Bitmap(imageFile);
            imageDisplayAdjust(pboxSingle, bmp);
        }

        private void addImage_MultiBox_Demo()
        {

        }

        private void addImage_NineBox_Demo()
        {

        }

        #endregion

        #region 图像控件文件加载，正式环境

        private void addImage_SingleBox_Mode()
        {

        }

        private void addImage_MultiBox_Mode()
        {

        }

        private void addImage_NineBox_Mode()
        {

        }

        private void imageDisplayAdjust(PictureBox pbox, Bitmap bmp)
        {
            if (bmp.Width < pbox.Width)
            {
                pbox.SizeMode = PictureBoxSizeMode.CenterImage;
            }
            else
            {
                pbox.SizeMode = PictureBoxSizeMode.Zoom;
            }
        }

        #endregion

        //图像浏览控件图片加载
        private void imageSlider1ImagesAdd()
        {
            int n = 0;
            string strSearchPath = txtSearchPath.Text;

            var files = new DirectoryInfo(strSearchPath).GetFiles();
            var data = files.Select(d =>
            {
                if (d.Name.Contains(tboxFileName.Text))
                {
                    this.imageSlider1.Images.Add(Image.FromFile(d.FullName));
                    n += 1;
                }
                return new { Name = d.Name, Image = Image.FromFile(d.FullName), Paths = d.FullName };
            });

            gridControl1.BeginUpdate();
            gridControl1.DataSource = data;
            gridControl1.EndUpdate();

            //MessageBox.Show("n = " + n);

            ControlEvent();
        }

        //图像浏览控件图片加载
        private void gridControlImageDataAdd()
        {
            string strSearchPath = txtSearchPath.Text.ToString();


        }

        public byte[] SaveImage(String path)
        {
            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read); //将图片以文件流的形式进行保存
            BinaryReader br = new BinaryReader(fs);
            byte[] imgBytesIn = br.ReadBytes((int)fs.Length); //将流读入到字节数组中
            return imgBytesIn;
        }

        #region 控件的委托事件

        /// <summary>
        /// 控件的委托事件
        /// </summary>
        private void ControlEvent()
        {
            /*卡片的点击事件*/
            this.layoutView1.CardClick += (s, e) =>
            {
                if (e.Button == MouseButtons.Left)
                    this.imageSlider1.CurrentImageIndex = e.Card.RowHandle;
            };
            this.sbtnFirst.Click += (s, e) =>
            {
                this.imageSlider1.SlideFirst();
            };
            this.sbtnPrev.Click += (s, e) =>
            {
                this.imageSlider1.SlidePrev();
            };
            this.sbtnNext.Click += (s, e) =>
            {
                this.imageSlider1.SlideNext();
            };
            this.sbtnLast.Click += (s, e) =>
            {
                this.imageSlider1.SlideLast();
            };
        }

        #endregion

        #region 切换显示方式

        private void rbtnSingleBox_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnSingleBox.Checked == true)
            {
                panelControlSingleBox.Dock = DockStyle.Fill;
                panelControlSingleBox.Visible = true;
                panelControlMultiBox.Width = 0;
                panelControlMultiBox.Visible = false;
                panelControlNineBox.Width = 0;
                panelControlNineBox.Visible = false;

                //pboxSingle显示gridControl1当前选择图片
                //To Do .......
            }
        }

        private void rbtnMultiBox_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnMultiBox.Checked == true)
            {
                panelControlSingleBox.Width = 0;
                panelControlSingleBox.Visible = false;
                panelControlMultiBox.Dock = DockStyle.Fill;
                panelControlMultiBox.Visible = true;
                panelControlNineBox.Width = 0;
                panelControlNineBox.Visible = false;

                //layoutControlMultiBox中所有图像控件显示gridControl1当前选择图片
                //To Do .......
            }
        }

        private void rbtnNineBox_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnNineBox.Checked == true)
            {
                panelControlSingleBox.Width = 0;
                panelControlSingleBox.Visible = false;
                panelControlMultiBox.Width = 0;
                panelControlMultiBox.Visible = false;
                panelControlNineBox.Dock = DockStyle.Fill;
                panelControlNineBox.Visible = true;

                //layoutControlNineBox中所有图像控件显示gridControl1当前选择图片
                //To Do .......
            }
        }

        #endregion

        #region 实例化标准功能

        public override void DoView(IButtonInfo sender)
        {
            sbtnView_Click(null, null);
            base.DoView(sender);
        }

        private void 隐藏选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 0;
        }

        private void 显示选项区ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panelTop.Height = 60;
        }

        #endregion

    }
}